const app = getApp()

Page({
  data: {

  },
  onLoad() {
    wx.loadFontFace({
      family: "Monaco",
      source: 'url("https://fonts.cdnfonts.com/s/14106/Monaco.woff")',
      scopes: ['webview', 'native'],
      success: (res) => {
        console.log('success', res.status)
        this.render()
      },
      fail: function (res) {
        console.error(res)
      },
      complete(res) {
        console.log('111', res)
      }
    });
  },

  tap() {
    wx.loadFontFace({
      family: "Monaco",
      source: 'url("https://fonts.cdnfonts.com/s/14106/Monaco.woff")',
      scopes: ['webview', 'native'],
      success: (res) => {
        console.log('success', res.status)
        this.render()
      },
      fail: function (res) {
        console.error(res)
      },
      complete(res) {
        console.log('111', res)
      }
    });

    this.createSelectorQuery().select("#view")
      .node().exec(res => {
        const node = res[0].node
        console.log("@@@@@@@@@ node:", node)

        node.takeSnapshot({
          type: 'arraybuffer',
          format: 'png',
          success: (res) => {
            console.log("@@ res:", res)
            const f = `${wx.env.USER_DATA_PATH}/hello.png`
            const fs = wx.getFileSystemManager();
            fs.writeFileSync(f, res.data, 'binary')
            wx.showToast({
              title: '保存成功'
            })

            this.setData({
              img: f
            })

            // setTimeout(() => {
            wx.saveImageToPhotosAlbum({
              filePath: f,
              complete(res) {
                console.log("@@ saveImageToPhotosAlbum 3:", res)
              }
            })
            // }, 1000)
            wx.getImageInfo({
              src: f,
              success(res) {
                console.log("@@ image info:", res)
              }
            })
          },
          fail(res) {
            console.log("@@@ fail:", res)
          }
        })
      })
  }
})
